//
//  YMLoginService.m
//  YMMedical
//
//  Created by y s on 2023/7/13.
//

#import "YMLoginService.h"
#import "YMNetwork.h"
#import "YMHomeTipInforAlert.h"
#import "YMRegisterViewController.h"

@implementation YMLoginService

/** 发送验证码 */
+ (void)requestUserSendCodeWithPhone:(NSString *)phone
                             success:(void(^)(void))success
                             failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/captchaSmsApp"];
    
    NSMutableDictionary *dictPara = [[NSMutableDictionary alloc] init];
    [dictPara setValue:[SDataUtil stringOf:phone valid:@""] forKey:@"phonenumber"];
    
    [YMNetwork getWithUrl:url parameters:dictPara success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (failure){
                failure(message);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}
/** 注册 */
+ (void)requestUserRegisterWithPhone:(NSString *)phone
                            password:(NSString *)password
                          confirmPwd:(NSString *)confirmPwd
                                code:(NSString *)code
                             success:(void(^)(void))success
                             failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/userRegister"];
    
    NSMutableDictionary *dictPara = [[NSMutableDictionary alloc] init];
    [dictPara setValue:[SDataUtil stringOf:phone valid:@""] forKey:@"username"];
    [dictPara setValue:[SDataUtil stringOf:password valid:@""] forKey:@"password"];
    [dictPara setValue:[SDataUtil stringOf:confirmPwd valid:@""] forKey:@"confirmPassword"];
    [dictPara setValue:[SDataUtil stringOf:code valid:@""] forKey:@"code"];
    [dictPara setValue:@"app_user" forKey:@"userType"];
    
    [YMNetwork postWithUrl:url parameters:dictPara success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            NSDictionary *dict = [SDataUtil dictOf:data[@"data"]];
            [[YMMyModel shared] loginSuccess:dict];
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (failure){
                failure(message);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}
/** 短信登录 */
+ (void)requestUserSmsLoginWithPhone:(NSString *)phone
                             smsCode:(NSString *)smsCode
                             success:(void(^)(void))success
                             failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/smsLoginApp"];
    
    NSMutableDictionary *dictPara = [[NSMutableDictionary alloc] init];
    [dictPara setValue:[SDataUtil stringOf:phone valid:@""] forKey:@"phonenumber"];
    [dictPara setValue:[SDataUtil stringOf:smsCode valid:@""] forKey:@"smsCode"];
    
    [YMNetwork postWithUrl:url parameters:dictPara success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            NSDictionary *dict = [SDataUtil dictOf:data[@"data"]];
            [[YMMyModel shared] loginSuccess:dict];
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (code == 201){
                //去注册
                [YMLoginService jumpToRegisterWithMsg:message phone:phone];
            }else{
                if (failure){
                    failure(message);
                }
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}
/** 账号密码登录 */
+ (void)requestUserAccountLoginWithPhone:(NSString *)phone
                                password:(NSString *)password
                                 success:(void(^)(void))success
                                 failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/loginApp"];
    
    NSMutableDictionary *dictPara = [[NSMutableDictionary alloc] init];
    [dictPara setValue:[SDataUtil stringOf:phone valid:@""] forKey:@"username"];
    [dictPara setValue:[SDataUtil stringOf:password valid:@""] forKey:@"password"];
    
    [YMNetwork postWithUrl:url parameters:dictPara success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            NSDictionary *dict = [SDataUtil dictOf:data[@"data"]];
            [[YMMyModel shared] loginSuccess:dict];
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (code == 201){
                //去注册
                [YMLoginService jumpToRegisterWithMsg:message phone:phone];
            }else{
                if (failure){
                    failure(message);
                }
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}
/** 登出 */
+ (void)requestUserLogoutAppSuccess:(void(^)(void))success
                            failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/logoutApp"];
    
    NSMutableDictionary *dictPara = [[NSMutableDictionary alloc] init];
    [YMNetwork postWithUrl:url parameters:dictPara success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (failure){
                failure(message);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}

/** 忘记密码接口 */
+ (void)requestUserForgetPwdWithPhone:(NSString *)phone
                             password:(NSString *)password
                           confirmPwd:(NSString *)confirmPwd
                                 code:(NSString *)code
                              success:(void(^)(void))success
                              failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/updatePwd"];
    
    NSMutableDictionary *dictPara = [[NSMutableDictionary alloc] init];
    [dictPara setValue:[SDataUtil stringOf:phone valid:@""] forKey:@"username"];
    [dictPara setValue:[SDataUtil stringOf:password valid:@""] forKey:@"password"];
    [dictPara setValue:[SDataUtil stringOf:confirmPwd valid:@""] forKey:@"confirmPassword"];
    [dictPara setValue:[SDataUtil stringOf:code valid:@""] forKey:@"code"];
    
    [YMNetwork postWithUrl:url parameters:dictPara success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (failure){
                failure(message);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}
/** 修改密码 */
+ (void)requestUserUpdateOldPwdWithPassword:(NSString *)password
                                 confirmPwd:(NSString *)confirmPwd
                                oldPassword:(NSString *)oldPassword
                                    success:(void(^)(void))success
                                    failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/updateOldPwd"];
    
    NSMutableDictionary *dictPara = [[NSMutableDictionary alloc] init];
    [dictPara setValue:[SDataUtil stringOf:password valid:@""] forKey:@"password"];
    [dictPara setValue:[SDataUtil stringOf:confirmPwd valid:@""] forKey:@"confirmPassword"];
    [dictPara setValue:[SDataUtil stringOf:oldPassword valid:@""] forKey:@"oldPassword"];
    
    [YMNetwork postWithUrl:url parameters:dictPara success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (failure){
                failure(message);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}

// 1:用户协议2:隐私政策
+ (void)requestUserProtocolWithType:(NSInteger)type
                            success:(void(^)(NSString *titleStr,NSString *contentStr))success
                            failure:(void(^)(NSString *message))failure{
    
    NSString *url = [KTuoToUrl stringByAppendingString:@"/appsheets/sheetapi/getFormList"];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"13aa4626c7014c1b93136c038019db8c" forKey:@"applicationid"];
    [dict setValue:@"19bfbf3e55fc436fb56b6e8b58b8b51b"forKey:@"formheaduuid"];
    [dict setValue:@0 forKey:@"needpage"];
    [dict setValue:@1 forKey:@"page"];
    [dict setValue:@20 forKey:@"pagesize"];
    
    NSMutableDictionary *dictField1 = [[NSMutableDictionary alloc] init];
    NSString *languageStr = [YMLanguageUtil isEnglish] ? @"d188f4a0-52dd-4e19-806e-f0ae8087765b":@"1268b839-540d-4eb5-b69a-d923b7fc98f6";
    [dictField1 setValue:@"xjxmcth0zw6isyy9" forKey:@"key"];
    [dictField1 setValue:languageStr forKey:@"value"];
    
    if ([YMLanguageUtil isEnglish]){
        if (type == 1){
            [dict setValue:@[@{@"key":@"qazczzminvqag3uh",
                               @"value":@"46a7d960-dd88-42aa-8f0a-f55ab4cc8bc8"},
                             dictField1] forKey:@"filters"];
        }else{
            [dict setValue:@[@{@"key":@"qazczzminvqag3uh",
                               @"value":@"e1bfc897-248e-4b84-9720-b93222f77406"},
                             dictField1] forKey:@"filters"];
        }
    }else{
        if (type == 1){
            [dict setValue:@[@{@"key":@"qazczzminvqag3uh",
                               @"value":@"770a5bc1-7f01-42a1-b9c1-0eb71b998fe0"},
                             dictField1] forKey:@"filters"];
        }else{
            [dict setValue:@[@{@"key":@"qazczzminvqag3uh",
                               @"value":@"a6e31548-e159-4e2e-8bcd-fa128608bd1e"},
                             dictField1] forKey:@"filters"];
        }
    }
    
    [YMNetwork postWithUrl:url parameters:dict success:^(id  _Nonnull data) {
        NSString *status = [SDataUtil stringOf:data[@"status"]];
        if ([YMNetwork tuoTuSuccess:status]){
            NSDictionary *dict = [SDataUtil dictOf:data[@"result"]];
            NSString *titleStr = @"";
            NSString *contentStr = @"";
            if (dict != nil){
                NSArray *arrTemp = [SDataUtil arrayOf:dict[@"data"]];
                if (arrTemp.count > 0){
                    NSDictionary *dictTemp = [SDataUtil dictOf:arrTemp.firstObject];
                    if (dictTemp != nil){
                        titleStr = [SDataUtil stringOf:dictTemp[@"qazczzminvqag3uh"] valid:@""];
                        contentStr = [SDataUtil stringOf:dictTemp[@"gfops0pjlbg9nlsq"] valid:@""];
                    }
                }
            }
            if (success){
                success(titleStr,contentStr);
            }
        }else{
            NSString *msg = [SDataUtil stringOf:data[@"err_msg"] valid:KLanguage(@"net_error_later_retry")];
            if (failure){
                failure(msg);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}


/** 未登录时 获取脱兔token */
+ (void)requestNoLoginGetTuoTuTokenSuccess:(void(^)(void))success
                                   failure:(void(^)(NSString *message))failure {
    
    NSString *url = [kBaseUrl stringByAppendingString:@"/app/user/getTuoTuToken"];
    
    [YMNetwork postWithUrl:url parameters:@{} success:^(id  _Nonnull data) {
        NSInteger code = [SDataUtil intOf:data[@"code"] valid:-1];
        if (code == 200){
            NSDictionary *dictData = [SDataUtil dictOf:data[@"data"]];
            NSString *tuoTuToken = [SDataUtil stringOf:dictData[@"tuoTuToken"] valid:@""];
            [YMMyModel shared].noLoginTuoTuToken = tuoTuToken;
            if (success){
                success();
            }
        }else{
            NSString *message = [SDataUtil stringOf:data[@"msg"]];
            if (failure){
                failure(message);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (failure){
            failure(KLanguage(@"net_error_later_retry"));
        }
    }];
}

+ (void)jumpToRegisterWithMsg:(NSString *)msg phone:(NSString *)phone{
    YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
    alert.iconName = @"alert_tips_infor_icon";
    alert.strTitle = KLanguage(@"hint_txt");
    alert.strContent = msg;
    alert.sureBlock = ^{
        YMRegisterViewController *vc = [[YMRegisterViewController alloc] init];
        vc.phoneStr = phone;
        vc.hidesBottomBarWhenPushed = YES;
        [SJumpUtil pushVC:vc animated:YES];
    };
    [alert show];
}


@end
